
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <assert.h>
#include <time.h>

#include "GameManager.h"

#include "GL/glut.h"


int m_windowCenter[2];
int m_windowSize[2];
int m_upKeyDepressed;
int m_downKeyDepressed;
int m_leftKeyDepressed;
int m_rightKeyDepressed;
int m_jumpKeyDepressed;
int frameCounter = 0;
float m_xyAspect;
GameManager * m_gameManager;



void GlutDisplay()
{

	if(m_gameManager->exit)
	{
		//delete(m_gameManager);
		//m_gameManager = NULL;
		exit(0);
	}
		
	glClearColor( 0,0,0, 1.0f );	
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	//Draw frame here
	
	//m_gameManager->Move();
	//m_gameManager->Interact();
	m_gameManager->PrepareFrame();
	//m_gameManager->HandleUserInput();
	m_gameManager->Draw();
	
	
	glutSwapBuffers();
	glutPostRedisplay();
	frameCounter++;
	//if(frameCounter > 10000)
	//	exit(0);
	//printf("exit myGlutDisplay\n");
  
}

void GlutReshape(int x, int y)
{
  m_xyAspect = (float)x / (float)y;
  glViewport( 0, 0, x, y );

   glutPostRedisplay();
}

void GlutKeyboardUp(unsigned char key, int x, int y)
{
	m_gameManager->SetUserInput(key,false);
	switch(key)
	{
		
		case 'w': m_upKeyDepressed = 0; break;
		case 's': m_downKeyDepressed = 0; break;
		case 'a': m_leftKeyDepressed = 0; break;
		case 'd': m_rightKeyDepressed = 0; break;
		case ' ': m_jumpKeyDepressed = 0; break;
		//case GLUT_KEY_UP: upk_depressed = 0; break;
		//case GLUT_KEY_DOWN: downk_depressed = 0; break;
	}
}
void GlutKeyboard(unsigned char key, int x, int y)
{
	m_gameManager->SetUserInput(key,true);
	switch(key)
	{
		case 'w': m_upKeyDepressed = 1; break;
		case 's': m_downKeyDepressed = 1; break;
		case 'a': m_leftKeyDepressed = 1; break;
		case 'd': m_rightKeyDepressed = 1; break;
		case ' ': m_jumpKeyDepressed = 1; break;
		case 27: //Pause
			break;
  		//case 'e':
		//	exit(0);
			break;
		//case GLUT_KEY_UP: upk_depressed = 0; break;
		//case GLUT_KEY_DOWN: downk_depressed = 0; break;
	}
}
void GlutSpecialKeys(int key, int x, int y)
{
	m_gameManager->SetUserInput(key, true);
}
void GlutSpecialKeysUp(int key, int x, int y)
{
	m_gameManager->SetUserInput(key, false);
}
void GlutMotion(int x, int y)
{
	Vector2D size = m_gameManager->GetCameraSize();
	float xSize = size.x * 2.0;
	float ySize = size.y * 2.0;
	if(m_gameManager->IsPlaySuspended())
	{
		xSize = 100.0;
		ySize = 100.0;
	}
	int windowWidth = glutGet(GLUT_WINDOW_WIDTH);
	int windowHeight = glutGet(GLUT_WINDOW_HEIGHT);
	m_gameManager->SetUserInput((xSize * (float)x/(float)windowWidth), ySize - ( ySize * (float)y/(float)windowHeight));
}
void GlutPassiveMotion(int x, int y)
{
}
void GlutMouse(int button, int buttonState, int x, int y)
{
	Vector2D size = m_gameManager->GetCameraSize();
	printf("Size = (%f, %f)\n",size.x, size.y);
	printf("x = %d, y = %d\n", x, y);
	float xSize = size.x * 2.0;
	float ySize = size.y * 2.0;
	if(m_gameManager->IsPlaySuspended())
	{
		xSize = 100.0;
		ySize = 100.0;
	}
	int windowWidth = glutGet(GLUT_WINDOW_WIDTH);
	int windowHeight = glutGet(GLUT_WINDOW_HEIGHT);
	m_gameManager->SetUserInput(button, buttonState == GLUT_DOWN, (xSize * (float)x/(float)windowWidth/*m_windowSize[0]*/), ySize - ( ySize * (float)y/(float)windowHeight/*m_windowSize[1]*/));
}

void Init()
{
	m_gameManager = new GameManager();
}
int main(int argc, char* argv[])
{
	m_windowSize[0] = 1024;
	m_windowSize[1] = 768;
	m_windowCenter[0] = m_windowSize[0]/2;
  	m_windowCenter[1] = m_windowSize[1]/2;
	m_upKeyDepressed = 0;
	m_downKeyDepressed = 0;
	m_leftKeyDepressed = 0;
	m_rightKeyDepressed = 0;
	m_jumpKeyDepressed = 0;
	
	printf("Begin\n");
	glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);// | GLUT_STENCIL);
	glutInitWindowPosition( 50, 50 );
  	glutInitWindowSize(m_windowSize[0], m_windowSize[1] );
  	glutInit(&argc, argv);
  	//glutGameModeString( "640x480:32@60" );
  	//glutEnterGameMode(); 
  	glutCreateWindow("Lincoln!");

 	
	glutDisplayFunc( GlutDisplay );
 	glutReshapeFunc( GlutReshape );  
 	glutKeyboardUpFunc( GlutKeyboardUp);
 	glutKeyboardFunc( GlutKeyboard );
 	glutSpecialFunc(GlutSpecialKeys);
 	glutSpecialUpFunc(GlutSpecialKeysUp);
 	glutMotionFunc( GlutMotion );
 	glutPassiveMotionFunc( GlutPassiveMotion);
 	glutMouseFunc( GlutMouse );
 	glEnable(GL_DEPTH_TEST);
 	srand(time(NULL));
 	Init();
 	glutMainLoop();
  	return 0;
}

